using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for MapControl.
	/// </summary>
	public class MapControl : AdminUserControl
	{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox procedureLabel;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox subjectLabel;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.TextBox prefixLabel;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.TextBox suffixLabel;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.TextBox limitAccessToLabel;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label7;
    private FreeFlowAdministrator.AttachmentsControl attachmentsControl;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.TextBox tableNameLabel;
    private System.Windows.Forms.CheckBox delegateEventsCheckBox;
    private System.Windows.Forms.CheckBox delegateEventsBeforeLocalCheckBox;
    private System.Windows.Forms.Label currentSuffixLabel;
    private System.Windows.Forms.TextBox currentFolderSuffixLabel;
    private TextBox foldersLabel;
    private Label label9;
    private TextBox activeFoldersLabel;
    private Label label10;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MapControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.label1 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.label3 = new System.Windows.Forms.Label();
      this.procedureLabel = new System.Windows.Forms.TextBox();
      this.label2 = new System.Windows.Forms.Label();
      this.subjectLabel = new System.Windows.Forms.TextBox();
      this.label4 = new System.Windows.Forms.Label();
      this.prefixLabel = new System.Windows.Forms.TextBox();
      this.label5 = new System.Windows.Forms.Label();
      this.suffixLabel = new System.Windows.Forms.TextBox();
      this.label6 = new System.Windows.Forms.Label();
      this.limitAccessToLabel = new System.Windows.Forms.TextBox();
      this.attachmentsControl = new FreeFlowAdministrator.AttachmentsControl();
      this.panel1 = new System.Windows.Forms.Panel();
      this.foldersLabel = new System.Windows.Forms.TextBox();
      this.label9 = new System.Windows.Forms.Label();
      this.currentFolderSuffixLabel = new System.Windows.Forms.TextBox();
      this.currentSuffixLabel = new System.Windows.Forms.Label();
      this.delegateEventsBeforeLocalCheckBox = new System.Windows.Forms.CheckBox();
      this.delegateEventsCheckBox = new System.Windows.Forms.CheckBox();
      this.tableNameLabel = new System.Windows.Forms.TextBox();
      this.label8 = new System.Windows.Forms.Label();
      this.label7 = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.label10 = new System.Windows.Forms.Label();
      this.activeFoldersLabel = new System.Windows.Forms.TextBox();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Name:";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.Location = new System.Drawing.Point(120, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.Size = new System.Drawing.Size(600, 20);
      this.nameLabel.TabIndex = 1;
      this.nameLabel.Text = "nameLabel";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(8, 32);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(64, 16);
      this.label3.TabIndex = 2;
      this.label3.Text = "Procedure:";
      // 
      // procedureLabel
      // 
      this.procedureLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.procedureLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.procedureLabel.Location = new System.Drawing.Point(120, 32);
      this.procedureLabel.Name = "procedureLabel";
      this.procedureLabel.ReadOnly = true;
      this.procedureLabel.Size = new System.Drawing.Size(600, 13);
      this.procedureLabel.TabIndex = 3;
      this.procedureLabel.Text = "procedureLabel";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 48);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(48, 16);
      this.label2.TabIndex = 4;
      this.label2.Text = "Subject:";
      // 
      // subjectLabel
      // 
      this.subjectLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.subjectLabel.Location = new System.Drawing.Point(120, 48);
      this.subjectLabel.Name = "subjectLabel";
      this.subjectLabel.ReadOnly = true;
      this.subjectLabel.Size = new System.Drawing.Size(656, 13);
      this.subjectLabel.TabIndex = 5;
      this.subjectLabel.Text = "subjectLabel";
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(8, 64);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(72, 16);
      this.label4.TabIndex = 6;
      this.label4.Text = "Folder prefix:";
      // 
      // prefixLabel
      // 
      this.prefixLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.prefixLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.prefixLabel.Location = new System.Drawing.Point(120, 64);
      this.prefixLabel.Name = "prefixLabel";
      this.prefixLabel.ReadOnly = true;
      this.prefixLabel.Size = new System.Drawing.Size(600, 13);
      this.prefixLabel.TabIndex = 7;
      this.prefixLabel.Text = "prefixLabel";
      // 
      // label5
      // 
      this.label5.Location = new System.Drawing.Point(8, 80);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(72, 16);
      this.label5.TabIndex = 8;
      this.label5.Text = "Suffix length:";
      // 
      // suffixLabel
      // 
      this.suffixLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.suffixLabel.Location = new System.Drawing.Point(120, 80);
      this.suffixLabel.Name = "suffixLabel";
      this.suffixLabel.ReadOnly = true;
      this.suffixLabel.Size = new System.Drawing.Size(100, 13);
      this.suffixLabel.TabIndex = 9;
      this.suffixLabel.Text = "suffixLabel";
      // 
      // label6
      // 
      this.label6.Location = new System.Drawing.Point(8, 112);
      this.label6.Name = "label6";
      this.label6.Size = new System.Drawing.Size(88, 16);
      this.label6.TabIndex = 10;
      this.label6.Text = "Limit access to:";
      // 
      // limitAccessToLabel
      // 
      this.limitAccessToLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.limitAccessToLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.limitAccessToLabel.Location = new System.Drawing.Point(120, 112);
      this.limitAccessToLabel.Name = "limitAccessToLabel";
      this.limitAccessToLabel.ReadOnly = true;
      this.limitAccessToLabel.Size = new System.Drawing.Size(408, 13);
      this.limitAccessToLabel.TabIndex = 11;
      this.limitAccessToLabel.Text = "limitAccessToLabel";
      // 
      // attachmentsControl
      // 
      this.attachmentsControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.attachmentsControl.Location = new System.Drawing.Point(0, 199);
      this.attachmentsControl.Name = "attachmentsControl";
      this.attachmentsControl.Size = new System.Drawing.Size(728, 289);
      this.attachmentsControl.TabIndex = 12;
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.activeFoldersLabel);
      this.panel1.Controls.Add(this.label10);
      this.panel1.Controls.Add(this.foldersLabel);
      this.panel1.Controls.Add(this.label9);
      this.panel1.Controls.Add(this.currentFolderSuffixLabel);
      this.panel1.Controls.Add(this.currentSuffixLabel);
      this.panel1.Controls.Add(this.delegateEventsBeforeLocalCheckBox);
      this.panel1.Controls.Add(this.delegateEventsCheckBox);
      this.panel1.Controls.Add(this.tableNameLabel);
      this.panel1.Controls.Add(this.label8);
      this.panel1.Controls.Add(this.label7);
      this.panel1.Controls.Add(this.label5);
      this.panel1.Controls.Add(this.label2);
      this.panel1.Controls.Add(this.label3);
      this.panel1.Controls.Add(this.limitAccessToLabel);
      this.panel1.Controls.Add(this.subjectLabel);
      this.panel1.Controls.Add(this.label1);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Controls.Add(this.procedureLabel);
      this.panel1.Controls.Add(this.prefixLabel);
      this.panel1.Controls.Add(this.suffixLabel);
      this.panel1.Controls.Add(this.label4);
      this.panel1.Controls.Add(this.label6);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(728, 199);
      this.panel1.TabIndex = 13;
      // 
      // foldersLabel
      // 
      this.foldersLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.foldersLabel.Location = new System.Drawing.Point(120, 144);
      this.foldersLabel.Name = "foldersLabel";
      this.foldersLabel.ReadOnly = true;
      this.foldersLabel.Size = new System.Drawing.Size(100, 13);
      this.foldersLabel.TabIndex = 20;
      this.foldersLabel.Text = "foldersLabel";
      // 
      // label9
      // 
      this.label9.AutoSize = true;
      this.label9.Location = new System.Drawing.Point(8, 144);
      this.label9.Name = "label9";
      this.label9.Size = new System.Drawing.Size(93, 13);
      this.label9.TabIndex = 19;
      this.label9.Text = "Number of folders:";
      // 
      // currentFolderSuffixLabel
      // 
      this.currentFolderSuffixLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.currentFolderSuffixLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.currentFolderSuffixLabel.Location = new System.Drawing.Point(120, 96);
      this.currentFolderSuffixLabel.Name = "currentFolderSuffixLabel";
      this.currentFolderSuffixLabel.ReadOnly = true;
      this.currentFolderSuffixLabel.Size = new System.Drawing.Size(408, 13);
      this.currentFolderSuffixLabel.TabIndex = 18;
      this.currentFolderSuffixLabel.Text = "currentFolderSuffixLabel";
      // 
      // currentSuffixLabel
      // 
      this.currentSuffixLabel.Location = new System.Drawing.Point(8, 96);
      this.currentSuffixLabel.Name = "currentSuffixLabel";
      this.currentSuffixLabel.Size = new System.Drawing.Size(112, 16);
      this.currentSuffixLabel.TabIndex = 17;
      this.currentSuffixLabel.Text = "Current folder suffix:";
      // 
      // delegateEventsBeforeLocalCheckBox
      // 
      this.delegateEventsBeforeLocalCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.delegateEventsBeforeLocalCheckBox.Enabled = false;
      this.delegateEventsBeforeLocalCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.delegateEventsBeforeLocalCheckBox.Location = new System.Drawing.Point(536, 120);
      this.delegateEventsBeforeLocalCheckBox.Name = "delegateEventsBeforeLocalCheckBox";
      this.delegateEventsBeforeLocalCheckBox.Size = new System.Drawing.Size(184, 24);
      this.delegateEventsBeforeLocalCheckBox.TabIndex = 16;
      this.delegateEventsBeforeLocalCheckBox.Text = "Delegate events before local";
      // 
      // delegateEventsCheckBox
      // 
      this.delegateEventsCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.delegateEventsCheckBox.Enabled = false;
      this.delegateEventsCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.delegateEventsCheckBox.Location = new System.Drawing.Point(536, 104);
      this.delegateEventsCheckBox.Name = "delegateEventsCheckBox";
      this.delegateEventsCheckBox.Size = new System.Drawing.Size(184, 16);
      this.delegateEventsCheckBox.TabIndex = 15;
      this.delegateEventsCheckBox.Text = "Delegate events";
      // 
      // tableNameLabel
      // 
      this.tableNameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.tableNameLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.tableNameLabel.Location = new System.Drawing.Point(120, 128);
      this.tableNameLabel.Name = "tableNameLabel";
      this.tableNameLabel.ReadOnly = true;
      this.tableNameLabel.Size = new System.Drawing.Size(408, 13);
      this.tableNameLabel.TabIndex = 14;
      this.tableNameLabel.Text = "tableNameLabel";
      // 
      // label8
      // 
      this.label8.Location = new System.Drawing.Point(8, 128);
      this.label8.Name = "label8";
      this.label8.Size = new System.Drawing.Size(80, 16);
      this.label8.TabIndex = 13;
      this.label8.Text = "Table name:";
      // 
      // label7
      // 
      this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.label7.Location = new System.Drawing.Point(8, 180);
      this.label7.Name = "label7";
      this.label7.Size = new System.Drawing.Size(100, 16);
      this.label7.TabIndex = 12;
      this.label7.Text = "Attachments";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(728, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(152, 488);
      this.uiActionsControl.TabIndex = 14;
      // 
      // label10
      // 
      this.label10.AutoSize = true;
      this.label10.Location = new System.Drawing.Point(8, 160);
      this.label10.Name = "label10";
      this.label10.Size = new System.Drawing.Size(74, 13);
      this.label10.TabIndex = 21;
      this.label10.Text = "Active folders:";
      // 
      // activeFoldersLabel
      // 
      this.activeFoldersLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.activeFoldersLabel.Location = new System.Drawing.Point(120, 160);
      this.activeFoldersLabel.Name = "activeFoldersLabel";
      this.activeFoldersLabel.ReadOnly = true;
      this.activeFoldersLabel.Size = new System.Drawing.Size(100, 13);
      this.activeFoldersLabel.TabIndex = 22;
      this.activeFoldersLabel.Text = "activeFoldersLabel";
      // 
      // MapControl
      // 
      this.Controls.Add(this.attachmentsControl);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "MapControl";
      this.Size = new System.Drawing.Size(880, 488);
      this.panel1.ResumeLayout(false);
      this.panel1.PerformLayout();
      this.ResumeLayout(false);

    }
		#endregion

    private Map map;
    public void LoadMap(Map map)
    {
      this.map = map;
      nameLabel.Text = map.Name;
      procedureLabel.Text = map.ProcedureName;
      subjectLabel.Text = map.Subject;
      prefixLabel.Text = map.Prefix;
      suffixLabel.Text = map.SuffixSize.ToString();

      // current folder suffix only works with SQL Server currently
      currentFolderSuffixLabel.Visible = (map.Server.DatabaseType == DatabaseType.SqlServer);
      currentSuffixLabel.Visible = (map.Server.DatabaseType == DatabaseType.SqlServer);
      if (map.Server.DatabaseType == DatabaseType.SqlServer)
        currentFolderSuffixLabel.Text = map.CurrentFolderSuffix.ToString();
      
      limitAccessToLabel.Text = map.LimitAccessTo;
      tableNameLabel.Text = map.TableName;
      attachmentsControl.LoadAttachments(map.Attachments);
      foldersLabel.Text = map.Folders.Count.ToString();
      activeFoldersLabel.Text = map.ActiveFolderCount.ToString();

      // version 7.5
      delegateEventsCheckBox.Visible = map.Server.SchemaVersion >= SchemaVersion.Version75;
      delegateEventsCheckBox.Checked = map.DelegateEvents;
      delegateEventsBeforeLocalCheckBox.Visible = map.Server.SchemaVersion >= SchemaVersion.Version75;
      delegateEventsBeforeLocalCheckBox.Checked = map.DelegateEventsBeforeLocal;

      UIActionList actions = new UIActionList();
      actions.Add("Apply changes", "Apply any changes made to this map. Rename a map in the database before republishing a procedure", 
        new ActionExecuteDelegate(ApplyChanges));
      actions.Add("Refresh", "Refresh the details for the map", new ActionExecuteDelegate(RefreshMap));
      actions.Add("Delete", "Delete this map", new ActionExecuteDelegate(DeleteMap));
      if (!map.IsAdministration)
        actions.Add("Purge stages", "Remove any old stages from the map if they have no folder still at the stage", 
          new ActionExecuteDelegate(PurgeStages));
      if (map.Server.DatabaseType == DatabaseType.SqlServer)
        actions.Add("Reset folder suffix", "Reset the folder suffix so the suffix for new folders will start from 1",
          new ActionExecuteDelegate(ResetFolderSuffix));
      actions.Add("Attachments");
      actions.Add(attachmentsControl.ActionList);
      uiActionsControl.Actions = actions;
    }

    private string PurgeStages()
    {
      int count = map.PurgeStages();
      return count + " stages purged";
    }

    private string DeleteMap()
    {
      if (MessageBox.Show("Delete this map?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          map.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return null;
    }

    private string ApplyChanges()
    {
      using (new CursorHandler())
      {
        map.Name = nameLabel.Text;
        map.ApplyChanges();
        map.Refresh();
        LoadMap(map);
        return Constants.ChangesApplied;
      }
    }

    private string RefreshMap()
    {
      map.Refresh();
      LoadMap(map);
      OnRefreshRequired();
      return null;
    }

    private string ResetFolderSuffix()
    {
      map.ResetFolderSuffix();
      map.Refresh();
      LoadMap(map);
      OnRefreshRequired();
      return "Folder suffix reset";
    }
	}
}
